package com.wch.clientdemo.module.base.network;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;

import android.content.Context;

/**
 * 
 * @author WanChengHe
 * @Date 20181115
 * 网络连接帮助类
 *
 */
public class NetworkHelper {

	private Context mcontext;
	private static NetworkHelper networkHelper = null;

	public NetworkHelper(Context context) {
		mcontext = context;
	}
	
	/**
	 * 获取NetworkHelper实例对象
	 * 采用单例模式
	 * @param context
	 * @return
	 */
	public static NetworkHelper getInstance(Context context){
		if(null == networkHelper){
			synchronized (NetworkHelper.class) {
				if(null == networkHelper){
					networkHelper = new NetworkHelper(context);
				}
			}
		}		
		return networkHelper;	
	}

	public Context getContext() {
		return mcontext;
	}
	
	
	public interface CallBackString {
		public void fail(VolleyError error);

		public void success(String response);
	}
	
	/**
	 * 获取请求队列
	 * @return
	 */
	private RequestQueue getRequestQueue(){
		return VolleyQueueController.getInstance(mcontext).getRequestQueue();
	}
	
	/**
	 * 结束特定context的网络请求
	 * @param context
	 */
	public void requestStop(Context context){
		getRequestQueue().cancelAll(context);
	}
	
	private void addRequest(Context context,Request<?> request,int timeoutMs){
		//重启策略：设置超时和重试请求	
		//initialTimeoutMs The initial timeout for the policy.
	    //maxNumRetries The maximum number of retries.
	    //backoffMultiplier Backoff multiplier for the policy.
		request.setRetryPolicy(new DefaultRetryPolicy(timeoutMs, timeoutMs, timeoutMs));
		request.setTag(context);
		getRequestQueue().add(request);
	}
	
	/**
	 * 发送给post请求，返回JsonString数据
	 * @param url
	 * @param params
	 * @param options
	 * @param call
	 * @param context
	 */
	public void stringPostRequest(String url,final Map<String, String> params,final VolleyOptions options,final CallBackString call,Context context ){
			StringRequest stringRequest = new StringRequest(Method.POST, url, new Response.Listener<String>() {

				@Override
				public void onResponse(String response) {
					call.success(response);
					
				}
			}, new Response.ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					call.fail(error);
					
				}
			}){

				@Override
				public Map<String, String> getHeaders() throws AuthFailureError {
					String sessionId = options.getSessionId();
					if(null == sessionId){
						HashMap<String, String> header = new HashMap<String,String>();
						header.put("cookie", sessionId);
						return header;
					}else{
						return super.getHeaders();
					}
				}

				@Override
				protected Map<String, String> getParams()
						throws AuthFailureError {
					
					return params;
				}
				
				
			};
			
			int timeout = options.getTimeoutMill();
			addRequest(context, stringRequest, timeout);
	}
	
}
